#include <stdio.h>
#include <ctype.h>
#include "defines.h"
#include <string.h>

int getch(void);
void ungetch(int);
int isletter(char c);
/* returns 1 if c is the character, 0 otherwise */
int cis(char ,char );
int isword(int c, char w[] );
int cmpword(char[], char[] );

   /* getop:  get next character or numeric operand */
   int getop(char s[])
   {
       if (!isdigit(s[0]) && s[0] != '.')
	    {
		 if (isletter(s[0]))
		   {
			//can't help but feel that this could be a switch.
			if (cmpword(s, "exp"))
			  return EXP;
			if (cmpword(s, "peek"))
			  return PEEK;
			if (cmpword(s, "pow"))
			  return POW;
			if (cmpword(s, "sin"))
			  return SINE;
			if (cmpword(s, "set"))
			  return SET;
			if (cmpword(s, "get"))
			  return GET;
			if (cis(s[0],'S'))
			  return SWAP;
			if (cis(s[0],'C'))
			  return CLEAR;
			if (cis(s[0],'D'))
			  return DUPE;
			//none of the above
		   }
		 return s[0];
	    }
       return NUMBER;
   }

int cis(char c,char b)
{
  if (c >'A' && c <='Z')
    return ( (c==b) || (b==c+32 ) );
  else if (c >'a' && c<= 'z')
    return ( (c==b) || (b==c-32 ) );
  else return 0;
}

int isletter(char c)
{
  return ((c> 'A' && c<='Z') ||  (c>'a' && c <='z')) ;
}

//checks if two strings are equal, if so returns 1 otherwise 0 */
int cmpword(char source[], char target[])
{
  return strcmp(source,target)==0;
}


//deprecated
int isword(int c, char w[] )
{
  char k=c;
  // var ;how far did we get;
  // count letters in word w = n
  // for i = 0, to i = n
  // getch
  // check if it matches
  // if so?  keep checking
  // if not? ungetch() that many times
     // for j = 0 ; j < thatmanytimes; j++
         //ungetch()
  int howmuch=0,l=0,m=0;

  for(int i=0; w[i]!='\0'; i++)
    {
	 howmuch=i;
	 l=i;
    }
    
  for(int i=0; i <l+1; i++)
	   {
		if ( isletter(k) && cis(k,w[i]))
		  {
		    k = getch();
		    m++;
		  }
	   }

  //@might be off by one here.
  //  if (m>0 && k>0)
  //  ungetch(k); // <- this is totally unnecessary. it grabs the end of 'sine'
  return (m==l+1);
}


